<?php
declare (strict_types=1);

namespace app\common\model;

use ffhome\common\util\CommonUtil;
use ffhome\util\JwtAuth;
use think\facade\Cache;
use think\facade\Db;

class AclUser extends \ffhome\frame\model\AclUser
{
    /**
     * 员工类别
     */
    const T_EMPLOYEE = 1;
    /**
     * 会员
     */
    const T_MEMBER = 2;
    /**
     * 登录缓存名称
     */
    const LOGIN_TIME_CACHE_NAME = 'login_time_';

    public static function update($id, $data)
    {
        Db::name(self::MODEL_NAME)->where(['id' => $id])->update($data);
    }

    /**
     * 更新登陆时间及登陆次数
     * @param $id
     */
    public static function updateLogin($id)
    {
        //缓存最大时间一天  60*60*24=86400
        Cache::set(self::LOGIN_TIME_CACHE_NAME . $id, date('Y-m-d'), 86400);
        self::update($id, ['login_time' => date('Y-m-d H:i:s'), 'login_num' => Db::raw('login_num + 1')]);
    }

    /**
     * 每天更新一次会员登陆时间
     * @param $id
     */
    public static function updateLoginEveryday($id)
    {
        $data = Cache::get(self::LOGIN_TIME_CACHE_NAME . $id);
        if (empty($data) || $data != date('Y-m-d')) {
            self::updateLogin($id);
        }
    }

    /**
     * 使用手机号码登录，找不到就创建用户
     * @param $mobile
     * @param $parentId
     * @param $openid
     */
    public static function loginByMobile(string $mobile, string $parentId, string $openid = '')
    {
        $user = Db::name(self::MODEL_NAME)->field('id,avatar,username,nickname,mobile')
            ->where(['mobile' => $mobile, 'type' => self::T_MEMBER])->find();
        if (!empty($user)) {
            self::updateLogin($user['id']);
            return self::generateToken($user);
        }
        $name = 'wx_' . base_convert(mt_rand(1000000, 9999999), 10, 36);
        $now = date('Y-m-d H:i:s');
        $user = [
            'type' => self::T_MEMBER,
            'mobile' => $mobile,
            'username' => $name,
            'nickname' => $name,
            'avatar' => sysValue('default_avatar'),
            'parent_id' => $parentId,
            'openid' => $openid,
            'create_time' => $now,
            'update_time' => $now,
        ];
        $user['id'] = Db::name(self::MODEL_NAME)->insertGetId($user);
        unset($user['type']);
        unset($user['openid']);
        unset($user['create_time']);
        unset($user['update_time']);
        return self::generateToken($user);
    }

    public static function generateToken(array $user)
    {
        $user['token'] = JwtAuth::getToken(['id' => $user['id']], sysValue('token_key'));
        return $user;
    }

    public static function loginByAccount(string $username, string $password)
    {
        $user = Db::name(self::MODEL_NAME)->field('id,avatar,username,nickname,mobile')
            ->where(['username' => $username, 'password' => CommonUtil::password($password), 'type' => self::T_MEMBER])
            ->find();
        if (empty($user)) return false;
        self::updateLogin($user['id']);
        return self::generateToken($user);
    }

    public static function getById(int $id, $fields = 'id,avatar,username,nickname')
    {
        return Db::name(self::MODEL_NAME)->field($fields)->where(['id' => $id])->find();
    }
}